[solved] list posts // all | by tags

matto
Jr. Bludit
Posts: 9
Joined: Fri Feb 19, 2016 6:50 pm

Fri Feb 19, 2016 6:57 pm

Hey!

This is how i get a list of all post or all posts of one tag:

Code: Select all

        if ($Url->whereAmI() == 'blog') {
            // list all posts
            $totalPublishedPosts = $dbPosts->numberPost(true);
            $posts = buildPostsForPage(0, $totalPublishedPosts, true, false);
        } elseif ($Url->whereAmI() == 'tag') {
            $tagKey = $Url->slug();
            $totalPublishedPosts = $dbTags->countPostsByTag($tagKey);
            $posts = buildPostsForPage(0, $totalPublishedPosts, true, $tagKey);
        }
if i want to exclude one tag from the list, this is how i do it:

Code: Select all

        $totalPublishedPosts = $dbPosts->numberPost(true);
        $posts = buildPostsForPage(0, $totalPublishedPosts, true, false);
        foreach ($posts as $Post) {
            if ( $Post->tags() != 'DONTLISTME' ) {
                echo '<a href="'.$Post->permalink() .'">';
                if($Post->coverImage()) {
                    echo '<img src="'.$Post->coverImage().'" alt="Cover Image">';
                } else {
                    echo '<h4>'.$Post->title().'</h4>';
                }
                echo '</a>';
            }
        }
and finally:
This i how a build a list with posts tagged with two keys (or more, just add conditions):

Code: Select all

            $totalPublishedPosts = $dbPosts->numberPost(true);
            $posts = buildPostsForPage(0, $totalPublishedPosts, true, false);
            // build a list of filtered posts
            $filteredPosts = [];
            foreach ($posts as $Post) {
                if ( (strpos($Post->tags(), 'TAG1') !== false) || (strpos($Post->tags(), 'TAG2') !== false)) {
                    $filteredPosts[] = $Post;
                }
            }

Have fun!
matto
------------------------------------

Hi!

I am creating a bludit theme and need too list all posts.

Code: Select all

            foreach($posts as $Post) {
                if($Post->published()) {
                    echo '<h1><a href="'.$Post->permalink().'">'.$Post->title().'</a></h1>';
                }
            }
does the job only on the start/first page.

What is correct way, to do this?

greetings
Last edited by matto on Thu Apr 06, 2017 10:58 am, edited 2 times in total.
User avatar
diego
Site Admin
Posts: 635
Joined: Sat May 16, 2015 2:53 pm
Contact:

Sat Feb 20, 2016 12:36 am

Hi,
try with this code:

Code: Select all

$totalPublishedPosts = $dbPosts->numberPost();

$posts = buildPostsForPage(0, $totalPublishedPosts, true, false);

foreach($posts as $Post) {
	echo '<h1><a href="'.$Post->permalink().'">'.$Post->title().'</a></h1>';
}
matto
Jr. Bludit
Posts: 9
Joined: Fri Feb 19, 2016 6:50 pm

Sat Feb 20, 2016 3:20 pm

Thanks, it helped me a lot!

I had to call

Code: Select all

numberPost();
with parameter true, otherwise it would have returned zero.
ccbc
Ssr. Bludit
Posts: 12
Joined: Tue Mar 01, 2016 1:22 pm

Tue Mar 01, 2016 1:24 pm

Where do I need to use that code?

Can I add something to a Page so it list all posts by date (newest first) ?

Let me know since it seems to be the only feature to miss for me!
matto
Jr. Bludit
Posts: 9
Joined: Fri Feb 19, 2016 6:50 pm

Wed Mar 02, 2016 2:11 pm

Hi!

Yes this is exactly what i use it for,
I simply use this in php/home.php

Code: Select all

    $totalPublishedPosts = $dbPosts->numberPost(true);
    $posts = buildPostsForPage(0, $totalPublishedPosts, true, false);
    foreach ($posts as $Post):
    
Greetings
ccbc
Ssr. Bludit
Posts: 12
Joined: Tue Mar 01, 2016 1:22 pm

Wed Mar 02, 2016 8:14 pm

This is to list all posts on the home page.

With about 200 posts, the home page feel overloaded. What I want to do is create a custom page from Bludit panel and have the list of all posts in a separate page, not the home page.

May be it can be handled by a new plugin.
ccbc
Ssr. Bludit
Posts: 12
Joined: Tue Mar 01, 2016 1:22 pm

Thu Mar 23, 2017 12:16 am

Anybody has a solution to list all posts?

I am looking for a plugin that will create a new page with all posts by descending date.
User avatar
Edi
Site Admin
Posts: 1261
Joined: Sun Aug 09, 2015 5:01 pm
Location: Zurich
Contact:

Thu Mar 23, 2017 1:28 pm

ccbc wrote:With about 200 posts, the home page feel overloaded. What I want to do is create a custom page from Bludit panel and have the list of all posts in a separate page, not the home page.
Also a separate page with about 200 posts will be overloaded... ;)

But why not use the given code in an own template and add an if condition to the index.php of the theme?
Planet Bludit, Tipps, Snippets und nützliche Links. - Newsletter, Informationen zu Bludit (auf Deutsch).
ccbc
Ssr. Bludit
Posts: 12
Joined: Tue Mar 01, 2016 1:22 pm

Sat Mar 25, 2017 6:42 am

Hi! I'm trying to add something to list all post titles with a link to open the individual post, not having 200 posts listed with their content :)

I tried adding an else in the theme index.php but it doesn't work. Any idea?

Code: Select all

<!doctype html>
<html lang="<?php echo $Site->language() ?>">
<head>
<?php include(PATH_THEME_PHP.'head.php') ?>
</head>
<body>
<?php Theme::plugins('siteBodyBegin') ?>
<div class="wrapper">
  <input type="checkbox" id="nav-button" class="nav-button" />
  <label for="nav-button">MENU</label>
  <section class="site-header">
    <?php include(PATH_THEME_PHP.'sidebar.php') ?>
  </section>
  <?php
    if( ($Url->whereAmI()=='home') || ($Url->whereAmI()=='tag') )
    {
      include(PATH_THEME_PHP.'home.php');
    }
    elseif($Url->whereAmI()=='post')
    {
      include(PATH_THEME_PHP.'post.php');
    }
    elseif($Url->whereAmI()=='page')
    {
      include(PATH_THEME_PHP.'page.php');
    }
    else
    {
        $totalPublishedPosts = $dbPosts->numberPost();

        $posts = buildPostsForPage(0, $totalPublishedPosts, true, false);
    
        foreach($posts as $Post) 
        {
            echo '<h1><a href="'.$Post->permalink().'">'.$Post->title().'</a></h1>';
        }
    }
  ?>
  <section class="site-footer">
    <p><?php echo $Site->footer() ?></p>
  </section>
</div>
<?php Theme::plugins('siteBodyEnd') ?>
</body>
</html>
User avatar
Edi
Site Admin
Posts: 1261
Joined: Sun Aug 09, 2015 5:01 pm
Location: Zurich
Contact:

Sat Mar 25, 2017 2:18 pm

You cannot use "else" in this case because there has to be an additional condition for the case when a list of all posts has to be shown (for example the title of the page with all posts). I also recommend using an own template including the code you have used as "else".

For example:

Code: Select all

		<?php
			if( ($Url->whereAmI()=='home') || ($Url->whereAmI()=='tag') || ($Url->whereAmI()=='blog') ) {
				include(THEME_DIR_PHP.'home.php');
			}
			elseif($Url->whereAmI()=='post') {
				include(THEME_DIR_PHP.'post.php');
			}
			elseif($Url->whereAmI()=='page' && ($Page->title()=='All posts')) {
				include(THEME_DIR_PHP.'all_posts.php');
			}
			elseif($Url->whereAmI()=='page') {
				include(THEME_DIR_PHP.'page.php');
			}
		?>
Planet Bludit, Tipps, Snippets und nützliche Links. - Newsletter, Informationen zu Bludit (auf Deutsch).
Post Reply