I want to display all website pages on a single page in a tree structure (sort of like the existing sitemap). I can get a page by its ID, I can get children of a page, but how do I find pages that are in the root and do not have a parent? In other words, how do I get the first level on the tree?
EDIT: I found out that home page has ID = 1, and dashboard ID = 2. I hope it is like this in every installation of Concrete5...
OK, I found the answer in the code. This is how you get home page:
$home = Page::getByID(HOME_CID, 'RECENT');
And this is how you get the children:
$children = $home->getCollectionChildren();
you can use
<?php echo View::url('/'); ?>
<?php echo View::url('/some-page'); ?>
Related
I'm creating my first wordpress theme, and I've looked around on google, and the wordpress codex for an answer to my question, but I can't seem to find exactly what I'm looking for, or couldn't get it working.
What I'm trying to do, or trying to figure out, is how I can make it so a certain category has a certain design.
So if I wanted to make an index.php for any music videos in "www.domain.com/music/trash/drake-song.mp4.html"
the trash category, it'd have its own design, but songs in
"www.domain.com/music/good-music/coldplay-viva-la-vida.mp4.html"
the good-music category, I want it to look pretty much completely different. I've tried using something similar inside my header.php to this;
<?php
if( is_tag( 'good-music' ) ):
$my_classes = array( 'good-class', 'good-class-two' );
else:
$my_classes = array( 'not-good-class' );
endif;_
?>
but it seemed to simply change the category page.
"www.domain.com/categories/good-music"
Anyone know what I could be doing wrong? I know basic html/css/php/javascript, new to creating a WordPress theme, and can't seem to get this working..
Also:
Using XAMPP to host locally, using Friendly URL's, properly configured
In order to generate category-specific markup for a single post layout, you can put code like the below in your single.php file after the call of get_header().
Please note that this checks for your category based upon category slug. So if your category slug (the url version of your category) does not match the first arguments in the in_array() function call below, then you should change the argument to match the slug for your category.
<?php
/* Start the Loop */
while ( have_posts() ) : the_post();
?>
<?php
$categories = get_the_category();
$catSlugs = array();
foreach ($categories as $category){
$catSlugs[] = $category->slug;
}
if (in_array('good-music',$catSlugs)){
$post_cat = 'good-music';
} else {
$post_cat = 'not-good-music';
}
get_template_part( 'template-parts/post/content', $post_cat );
?>
<?php
endwhile; // End of the loop.
?>
It is important that this code only appear where there is a query to loop against.
Specifically, the file single.php in your theme should be the default file for displaying a single post, regardless of category. When you navigate to the url of a single post, this layout should be triggered.
As part of that triggering, a wordpress query of just that post is returned to be looped through.
Then, the code from while ( have_posts() ) : the_post(); until endwhile; will run one time, because there is a single post to be processed.
If there were more than one post, such as on a category page or on your default post listing page, then the code inside of that while loop would run as many times as there are posts in the query for that layout.
If you were to place the code in the header, it won't work because the header is prepared independently of the loop that runs on this page.
You could run a custom WP_query() in the header, but that is rarely a good way to handle site content.
In this situation it would not be appropriate, because you are customizing content of existing posts, and only differentiating based upon category.
So, just use the standard layouts files with custom layout parts.
I stripped out the divs in the loop below, because you may or may not be using bootstrap.
After this code is placed on your page, you would create files called content-good-music.php and content-not-good-music.php in YOUR_THEME_DIR/template-parts/post/ directory. The key is that you would add whatever your category slug is to the end of your
These files will contain the unique markup for these kinds of posts. You can also use a similar logic in your post listing loop to give the listed posts for each category their own unique php files.
Here's some get_template_part() documentation.
https://developer.wordpress.org/reference/functions/get_template_part/
I am trying to load a Wordpress page in another php file. Since I have integrated CodeIgniter with Wordpress.
The following works without problems and loads the wordpress site.
get_header();
get_footer();
But how do I change which page is loaded, instead of the homepage?
I only want to call the get_header() and get_footer wp functions but find a way to select the page.
I have thus far tried altering the $wp_query global object but haven't had any success.
Thanks,
The following worked:
$page_to_load = new WP_Query(array(
'page_id' => $page_id
));
global $wp_query;
$wp_query = $page_to_load;
wp();
get_header();
get_footer();
This sets the page to load in the $wp_query object, and then you have to call wp() in order to reload the page and have it route to the page.
So you want to display the header and footer only on some pages. There are 2 functions, which should solve the problem.
First one:
is_front_page()
This one just tests, whether the displayed page is your front page. You just need to use it like this:
<?php
echo is_front_page() ? "I'm a front page." : "This isn't a front page"
?>
Second one: is_page()
There are multiple ways to use this function. First one (without any parameter) just tests, whether it is page. Second one (with an ID) only returns true, if the requested page has got the specific ID. Third one (with slug or post title) is the same like the second only with those parameters.
My homepage's title is being displayed as the title of the last blog post created.
The code is:
<title><?php bloginfo('name'); ?></title>
So from my understanding that should display the blog title (set in general settings) on the homepage.
But its not. Its displaying the most recent post title.
What do i need to look for?
wp_title() is used to display the title of the page being displayed, but it uses the query results to get its value. So if you are executing a loop on many posts (which you obviously are on your homepage) and you don't reset it, you will get the title of the last post in your loop... logical.
Besides, note that the homepage is index.php in your theme, it is not a real page in WordPress. So it hasn't got a title. So wp_title() can't be of any use for you here.
Basically, your homepage has not got any title. So if this template is both for your homepage and other pages, you need to do a conditional check :
Is this homepage? (use is_home())
A. Yes, echo "Welcome on my great website"
B. No, wp_title(), which will echo the title of the page you are on...
Do you get it?
First off, you're executing two functions here -- wp_title, which retrieves the title of the page the visitor is currently on, and thereafter bloginfo, which with the argument 'name' indeed fetches your blog's name as set in the configuration.
However, there's a slight error in your code; you'd get the desired result as follows:
<title>
<?php
wp_title('|', true, 'right');
bloginfo('name');
?>
</title>
You should read about the parameters for wp_title on the WP Codex; the | gives you a delimiter, for example, and 'right' tells the function where to output said separator.
Note: I'd advise you to display both the post title and blog name, as only the blog name on every single page is both unhelpful for visitors and yields por results in search engine results.
I found your problem! Error in your code : remove > after <?php (you have written <?php>) so this :
<?php> bloginfo('name'); ?>
should now become this :
<?php bloginfo('name'); ?>
And it will work !
It seems like a simple task, but I'm having difficult time finding any information on this. Basically I have an "Events" page created in wordpress to which I want to create a link on my main page. I do not want it to be static like (https://mysite.com/events). Is there a way to do it dynamically?
You want to use the get_permalink() function to write the link - http://codex.wordpress.org/Function_Reference/get_permalink
This function accepts an argument which is the post or page ID (and defaults to the ID of the current page), so if your Events page is ID #11, it would look like to following to also have the link text match the page title (should it change to "Events!" in the future, for example):
<?php $events_page = get_post(11); ?>
<?php echo $events_page->post_title; ?>
I use Barecity Theme for WordPress. i built everything, but now i need to add a single.php file to theme. now it displays all post content at homepage, I created a short_desc Custom Field. and I call it from code with;
<?php //get_post_meta($post->ID, 'short_desc', true); ?>
it is fine. but i need to display this short desc at home page listing, and the main content at details page. how can do that?
I appreciate helps!!
It sounds like what you are trying to do is show a custom field that you have set on a post on the index page (which lists all the posts).
To do that you'll need to modify index.php by adding your snippet where you would like to have the short description.
<?php echo get_post_meta($post->ID, 'short_desc', true); ?>
You need to use echo to display the results from the get_post_meta function.
Depending on how your posts are setup you can also use the More button when you write your posts. This will cut off your post at a certain point that you decide and only show that short part on the index and archive pages.
Another option would be to use
<?php the_excerpt(); ?>
Which shows the first 55 words (this can be adjusted though) of the post.
Hope that helps,
Paul