I'm creating my own wordpress theme which is a bit different because it will not have single pages (or atleast, no single page will be reachable). The whole website contains just the homepage (with the loop) and previous posts pages.
I want to link to individual posts inside the loop like site.com#post-124 or site.com/paged=5#post-214.
I already created a function that does this:
function getPermalink($id,$postsPerPage) {
$postNumber = Get_Post_Number($id);
//a function that get's the post number based on
//the chronical order of published posts.
$page = floor(($postNumber - 1) / $postsPerPage);
$url = get_option('home');
if($page > 0) {
$url .= '/?paged=' . ($page + (1 - floor($page / 5)));
}
$url .= '#post-' . $id;
return $url;
}
You can see it live here: http://mijnrealiteit.nl (the previous posts pages are replaced by an infite scroll plugin).
This works, however it breaks when I start adding posts because all the posts before will get shifted back to pages further away (this makes the link invalid).
The way I see it there are two possible solutions:
Change the permalinkstructure to display paging backwards (so x.com/paged=231 becomes the first 'previous' page. However this is not userfriendly.
Make links with just the ID and let wordpress handle custom redirection to the page at that current point in time.
Are there better alternatives? I'm sure this is already solved somewhere, I just couldn't find it.
I got pushed in the right direction by a friend, I build it quite easily using option 2:
The getPermalink function is now much simpler:
function getPermalink($id) {
return get_option('home') . '/?f=' . $id;
}
I didn't make any custom redirection, I just checked at the homepage for a an 'f' being passed in the GET request:
$perma = $_GET['f'];
if(isset($perma) && !is_paged()) {
$customposts = get_posts('p=' . $perma );
foreach( $customposts as $post ) :
setup_postdata($post); ?>
//load the post
<?php endforeach;
}?>
If that is true the post will be fetched using the get_posts function by Wordpress. I also check the (normal) loop for the post that already has been served:
<?php while (have_posts()) : the_post();
if(get_the_ID() != $perma) { ?>
//load the post
<?php } endwhile; ?>
Related
I'm using Wordpress as headless CMS.
I use the following function to return a page based on the page title.
No code or Wordpress content has changed and suddenly it has stopped working...
Almost indentical code on the same server on a different site is working fine.
Anyone got any ideas what might have happened ?
// -- Get page based on page name -- //
function getWordpressPage($wordpressPageName){
$page = NULL;
$pageRequest = cleanUrlVar($wordpressPageName);
if($pageRequest){
$page = get_page_by_title( $pageRequest );
if($page != NULL){
setup_postdata($page);
return the_content();
}
}
if($page == ''){
header('Location: /');
exit();
}
}
FYI - var_dump($page) returns the data so Wordpress is returning something, it looks like it's falling over at the 'return the_content() stage...
the_content() will show the content by echo it. So if you want to get
variable to return, you should use get_the_content()
Update your codes to return it:
return get_the_content();
You can find out more here:
https://developer.wordpress.org/reference/functions/get_the_content/
I'm generating some html to be used with the MPDF php library for generating a PDF. But when I create some HTML with php which has a loop inside it, it cuts of the beginning of the string, here's my code:
$page = "<div class='A4'><h1>Test: lookbook</h1><p>";
$page .= $date;
$page .= "</p><div class='items'>";
while ($the_query->have_posts()) {
$the_query->the_post();
$page .= "<img src='";
$page .= get_field('product_image')['url'];
$page .= "'>";
}
$page .= "</div></div><div class='footer'>A Story in every gemstone</div>";
echo $page;
Running the above code returns:
1<img src='urltoimage'></div></div><div class='footer'>A Story in every gemstone</div>
So it looks like everything before the while loop is being cut out.
$page is a Wordpress global variable that exists within The Loop.
https://codex.wordpress.org/Global_Variables
WordPress-specific global variables are used throughout WordPress code for various reasons. Almost all data that WordPress generates can be found in a global variable.
While inside the loop, these globals are set, containing information about the current post being processed.
...
$page (int) The page of the current post being viewed. Specified by the query var page.
Example:
We got some nice posts that got a link to jump to the next post with scrolling down the page. No reload, new tab or anything just a simple smoothscrolling of the page.
Problem:
We are inside a Wordpress Loop that creates some content from the DB and want to generate a link that jumps to the next run-through that will be generated by the Loop. Within the loop we cant predict what will be the next post/product or whatever post-type the loop should go through. So how am I giving the Link the correct anchor link?
Possible Solutions:
Maybe we can store the data from each run in an array?
Maybe we should create a second loop that only saves the data?
Maybe I should just use JS to rename that anchors after it finishes...
You can add an incremental id with the loop index and assign the anchor to the current index + 1. A simple example:
<?php
global $wp_query;
$args = array(
'post_type' => 'post'
);
$post_query = new WP_Query($args);
if($post_query->have_posts() ) {
while($post_query->have_posts() ) {
$post_query->the_post();
$index = $wp_query->current_post + 1;
?>
<div id="my_post_<?php echo $index; ?>"><?php the_title(); ?></div>
<?php
if (($wp_query->current_post +1) <= ($wp_query->post_count)) {
echo 'Next post';
}
?>
}
}
?>
I'm using WP, and have a script where when an image is clicked, the single post content loads using .load(). The arrows to navigate through each single post is located inside the .project div that is being loaded using .load().
Problem is, on the first and last posts, I only want to display certain arrows.
For example, the first item in the post gets loaded in, it shouldn't have the 'previous' arrow because there are no previous posts. Same with the last post and the 'next' arrow.
So basically to work around this, I'm trying to come up with a PHP statement (without being in the loop) to tell if the current post is the last post or first post in the custom post type.
Here's what I have so far.. just not sure how to get ID's of first post and last post outside of the loop. Everything else besides that has been tested and works. Below is just the 'logic' behind the code mostly.
<?php
// Get other posts in post type
$next_post = get_next_post();
$previous_post = get_previous_post();
$current_id = get_the_ID();
// Get ID's of posts
$next_id = $next_post->ID;
$previous_id = $previous_post->ID;
// if($next_id == $previous_id) because on first/last posts, get_next_post
// and get_previous_post return the same value.
if($next_id == $previous_id) {
if() {
// if last post in custom post type
} else() {
// if first post in custom post type
}
}
?>
<?php if(isnt first post) { ?>
<li class="left" data-projectid="<?php echo $next_id; ?>"></li>
<?php } ?>
<li class="grid"></li>
<?php if(isnt last post) { ?>
<li class="right" data-projectid="<?php echo $previous_id; ?>"></li>
<?php } ?>
I haven't worked with WP so much, but since WP templates are all PHP files and WP exposes its own API to user you can use any PHP syntax in them. If you're not concerned about running two queries each time you navigate your page then this will help you to get the idea.
<?php
global $wpdb;
$last_one = FALSE;
$first_one = FALSE;
// Get last one
$last_result = $wpdb->get_results("SELECT `id` FROM `posts` ORDER BY `id` DESC LIMIT 0, 1", ARRAY_A);
if($last_result){ if($last_result['id'] == $next_post){ $last_one = TRUE; } }
// Get first one
$first_result = $wpdb->get_results("SELECT `id` FROM `posts` ORDER BY `id` ASC LIMIT 0, 1", ARRAY_A);
if($first_result){ if($first_result['id'] == $previous_post){ $first_one = TRUE; } }
?>
Remember to check the names of fields and tables as I don't know the names.
Ended up using this code and it works fine...
Edit: updated code.. if for any reason somebody else ever needs it:
$args = array('post_type'=>'your_post_type', 'posts_per_page' => -1);
$posts = get_posts($args);
$first_id = $posts[0]->ID; // To get ID of first post in custom post type
// outside of loop
$last_id = end($posts);
echo $last_id->ID; // To get ID of last post in custom post type outside of loop
if($current_id != $first_id) { ?>
<li class="left" data-projectid="<?php echo $previous_id; ?>"></li>
<?php } ?>
<?php if($current_id != $last_id->ID) { ?>
<li class="right" data-projectid="<?php echo $next_id; ?>"></li>
<?php } ?>
I have a WordPress site for a client. He owns a video store, and I made a site for him to update the list of movies, usually just the "new this week" movies.
I used PodCMS as an easy way for him to upload movies and then display them. No need for him to even create posts. Works really well, it's a great extension, I'm just having some issues.
The Pod has a field where you insert the release date. 2010-04-20
Then there is a Pod page/template combo that calls the movies with a certain release date like this:
$date = pods_url_variable('last');
He then just creates a blank WP page with the slug 2010-04-20
So when you open that page, the Pod page/template reads that slug and renders a list of the appropriate movies.
My problem:
I need these to be searchable. Is this possible.
I'm also open to suggestions on other ways to go about making this site work. I need it to be as simple as that. Uploads some movies and creates a new page. Then the rest is done automatically.
A PodsCMS search is nothing more than a mySQL table search for the search term. You can search the title, body, pretty much anything. Here's an example:
Note: I'm using "whatever" as the pod info. I'm also forming a string that goes into the $where position which comprises the various pods variables I want to search on. Also, I'm assuming pagination using the Pods pagination controls but I want that variable carried across the pages so I can offset.
<?php
$search_term = $_GET["s"];
$paged = get_query_var('paged');
$page_number = $_GET['pg'];
?>
<h1>
Results for "<?php echo $search_term; ?>"<?php if($page_number > 1){ ?> (Continued)<?php } ?><?php if($paged > 1){ ?> (Continued)<?php } ?>
</h1>
<?php if($paged <= 1){ ?>
<h2>Results in Whatever...</h2>
<?php
$whateverSentence = "(t.name LIKE '%" .$search_term. "%') || (t.whatever LIKE '%" .$search_term. "%')";
$whatever = new Pod('whatever');
$whatever->findRecords($orderby = 't.whatever DESC', $rows_per_page = 5, $where = $whateverSentence, $sql = null);
$total_whatever = $whatever->getTotalRows();
?>
<?php if( $total_whatever >0 ) : ?>
<?php while ( $whatever->fetchRecord() ) : ?>
<?php
// Set Variables
$whatever_ID = $whatever->get_field('id');
$whatever_Permalink = $whatever->get_field('slug');
$whatever_Name = $whatever->get_field('name');
?>
Code that echos the pods variables and represents the search result
<?php endwhile ?>
<?php else: ?>
<p>Sorry, no results found for that search.</p>
<?php endelse; endif ?>
<?php echo $whatever->getPagination($label = '<span class="pagination-text">Go to page:</span>'); ?>
<?php } ?>
So you need the WordPress page content - the list of movies - to be searchable?
WP doesn't search pages natively, but will with WordPress › Search Everything « WordPress Plugins and search better with WordPress › Relevanssi « WordPress Plugins