API page.content - nur bis zum pagebreak anzeigen!

Post Reply
peterdot
Jr. Bludit
Posts: 8
Joined: Wed Jun 20, 2018 2:29 pm

Ich benutze die API mit Jquery und JSON, um die letzten Blogposts auf einer übergeordneten Seite anzuzeigen.
Das funktioniert soweit auch problemlos mit folgendem Code:

Code: Select all

<script>
        $(document).ready(function(){
                $.ajax({
                        url: "xxxxxxxxx",
                        method: "GET",
                        data: "token=xxxxxxxxxxxx",
                        dataType: 'json',
                        success: function(json) {
                                console.log(json);
                                // Print the title of the pages
                                $.each(json.data, function(index, page) {
                                        $("#latest_posts").append("<a href='" + page.permalink + "' title='" + page.title + "'><h3>" + page.title + "</h3></a>" + "<p>" + page.content + "</p><div><a href='" + page.permalink + "' class='badge badge-success' role='button' >Weiterlesen</a></div><hr>");
										
                                });
                        },
                        error: function(data) {
                                console.log("Error");
                        }
                });
        });
        </script>
Mein Problem ist nur: die API gibt mir den gesamten Inhalt des Posts zurück. Was ich aber bräuchte ist: den page.content nur bis zum pagebreak Tag.

In Jquery gibt es denke ich verschiedene Funktionen, um page.content bei einem bestimmten keyword ("pagebreak") abzuschneiden. Nur ich kenne mich mit JQUERY viel zu wenig aus. Vielleicht wäre es mit der slice Funktion möglich?
https://api.jquery.com/slice/

Oder gibt es eine Funktion in der API, um den Content zu begrenzen?

Vielen Dank für die Hilfe!
peterdot
Jr. Bludit
Posts: 8
Joined: Wed Jun 20, 2018 2:29 pm

Ich habe selber eine Lösung gebastelt mit der .split Funktion. Ist zwar nicht super elegant, aber funktioniert tadellos:

Code: Select all

<script>
        $(document).ready(function(){
                $.ajax({
                        url: "http://shiatsu-jetzt.at/blog/api/pages",
                        method: "GET",
                        data: "token=e184e7433b26c058822a5493c3947071",
                        dataType: 'json',
                        success: function(json) {
                                console.log(json);
                                
                                
								//each page
								$.each(json.data, function(index, page) {
								
								//put page.content into a variable
								var contentraw = page.content;
								
								//split the content at pagebreak into two parts
								var contentsplit = contentraw.split ("<!-- pagebreak");
								
								// put the first part into variable intro
								var intro = contentsplit[0];
								
                                $("#latest_posts").append("<a href='" + page.permalink + "' title='" + page.title + "'><h3>" + page.title + "</h3></a>" + "<p>" + intro + "</p><div><a href='" + page.permalink + "' class='badge badge-success' role='button' aria-disabled='true'>Weiterlesen</a></div><hr>");
										
                                });
                        },
                        error: function(data) {
                                console.log("Error");
                        }
                });
        });
        </script>


Das gibt mir statt dem page.content eben genau den content bis zum pagebreak tag aus.
User avatar
Edi
Site Admin
Posts: 3121
Joined: Sun Aug 09, 2015 5:01 pm
Location: Zurich
Has thanked: 54 times
Been thanked: 77 times
Contact:

Ich habe das mit Diego angeschaut. Wenn es so funktioniert, ist es okay. Die API anzupassen, wäre etwas problematisch.
Clickwork - Websites mit Bludit | Planet Bludit - Tipps und Snippets
Post Reply